/*
 * $RCSfile: AgentAction.java,v $
 * $Revision: 1.1 $
 * $Date: 2014-03-25 $
 *
 * Copyright (C) 2005 Skin, Inc. All rights reserved.
 *
 * This software is the proprietary information of Skin, Inc.
 * Use is subject to license terms.
 */
package com.skin.demon.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.skin.demon.ClassScanner;
import com.skin.demon.MethodInfo;
import com.skin.demon.PackageInfo;
import com.skin.j2ee.action.BaseAction;
import com.skin.j2ee.annotation.UrlPattern;
import com.skin.j2ee.util.Response;

/**
 * <p>Title: AgentAction</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * @author xuesong.net
 * @version 1.0
 */
public class AgentAction extends BaseAction {
    private static final Logger logger = LoggerFactory.getLogger(AgentAction.class);

    /**
     * @throws IOException
     * @throws ServletException
     */
    @UrlPattern("/webeye/app.html")
    public void tree() throws IOException, ServletException {
        this.forward("/template/webeye/app.jsp");
    }

    /**
     * @throws IOException
     */
    @UrlPattern("/webeye/agent/getTreeXml.html")
    public void getTreeXml() throws IOException {
        logger.info("AgentServlet.getTreeXml(request, response);");
        String[] packages = new String[]{"com.skin"};
        String contextPath = this.getContextPath();
        StringBuilder buffer = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
        buffer.append("<tree>\r\n");

        for(int i = 0; i < packages.length; i++) {
            String name = packages[i];
            buffer.append("<treeNode");
            buffer.append(" icon=\"package.gif\"");
            buffer.append(" title=\"");
            buffer.append(name);
            buffer.append("\"");
            buffer.append(" href=\"#\"");
            buffer.append(" nodeXmlSrc=\"");
            buffer.append(contextPath);
            buffer.append("/webeye/agent/getTreeXml2.html?packageName=");
            buffer.append(name);
            buffer.append("\"/>\r\n");
        }
        buffer.append("</tree>\r\n");
        Response.write(this.request, this.response, "text/xml; charset=utf-8", buffer.toString());
    }

    /**
     * @throws IOException
     * @throws ServletException
     */
    @UrlPattern("/webeye/agent/getTreeXml2.html")
    public void getTreeXml2() throws IOException, ServletException {
        String contextPath = this.getContextPath();
        String packageName = this.getTrimString("packageName");
        logger.info("packageName: {}", packageName);

        List<PackageInfo> childs = new ClassScanner().getChildList(packageName);
        StringBuilder buffer = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
        buffer.append("<tree>\r\n");

        for(PackageInfo classInfo : childs) {
            if(classInfo.getPackage()) {
                buffer.append("<treeNode");
                buffer.append(" icon=\"package.gif\"");
                buffer.append(" title=\"");
                buffer.append(classInfo.getName());
                buffer.append("\"");
                buffer.append(" nodeXmlSrc=\"");
                buffer.append(contextPath);
                buffer.append("/webeye/agent/getTreeXml2.html?packageName=");
                buffer.append(classInfo.getPackageName());
                buffer.append(".");
                buffer.append(classInfo.getName());
                buffer.append("\" href=\"");
                buffer.append(contextPath);
                buffer.append("/webeye/agent/detail.html?packageName=");
                buffer.append(classInfo.getPackageName());
                buffer.append(".");
                buffer.append(classInfo.getName());
                buffer.append("\"/>");
            }
            else {
                buffer.append("<treeNode");
                buffer.append(" icon=\"class.gif\"");
                buffer.append(" title=\"");
                buffer.append(classInfo.getName());
                buffer.append(".java\"");
                buffer.append(" href=\"");
                buffer.append(contextPath);
                buffer.append("/webeye/agent/detail.html?className=");
                buffer.append(classInfo.getPackageName());
                buffer.append(".");
                buffer.append(classInfo.getName());
                buffer.append("\"/>");
            }
        }
        buffer.append("</tree>\r\n");
        Response.write(this.request, this.response, "text/xml; charset=utf-8", buffer.toString());
    }

    /**
     * @throws ServletException
     * @throws IOException
     */
    @UrlPattern("/webeye/agent/detail.html")
    public void detail() throws ServletException, IOException {
        String packageName = this.getTrimString("packageName");
        String className = this.getTrimString("className");
        ClassScanner scanner = new ClassScanner();

        if(packageName.length() > 0) {
            List<String> locations = scanner.getLocations(packageName.replace('.', '/'));
            this.setAttribute("packageName", packageName);
            this.setAttribute("locations", locations);
        }
        else if(className.length() > 0) {
            String location = scanner.getLocation(className);
            List<String> locations = scanner.getLocations(className.replace('.', '/') + ".class");
            List<MethodInfo> methodInfoList = scanner.getMethodInfoList(className);
            this.setAttribute("className", className);
            this.setAttribute("location", location);
            this.setAttribute("locations", locations);
            this.setAttribute("methodInfoList", methodInfoList);
        }
        else {
            throw new java.lang.IllegalArgumentException("bad className: null");
        }
        this.forward("/template/webeye/detail.jsp");
    }
}
